package com.abctime.demo;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.abctime.api.ABCLibraryApi;
import com.abctime.api.PaymentJump;
import com.abctime.api.commonapi.IApiCallback;

public class ExchangeCodeActivity extends AppCompatActivity {

    private EditText mLevelIdEdit;
    private EditText mExchangeCodeEdit;
    private String uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exchange_code);

        init();

        setupExchange();
    }

    private void init() {
        Intent intent = getIntent();
        uid = intent.getStringExtra("uid");
        String levelId = intent.getStringExtra("levelId");
        String levelName = intent.getStringExtra("levelName");

        TextView tvMessage = findViewById(R.id.tv_message);
        tvMessage.setText(String.format("支付界面  uid = %s  levelId = %s  levelName = %s", uid, levelId, levelName));

        mLevelIdEdit = findViewById(R.id.et_level_id);
        mExchangeCodeEdit = findViewById(R.id.et_exchange_code);
    }

    private void setupExchange() {
        findViewById(R.id.tv_exchange_code).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String product_id = mLevelIdEdit.getEditableText().toString().trim();
                String exchangeCode = mExchangeCodeEdit.getEditableText().toString().trim();
                ABCLibraryApi.getApiService().exchangeCode(
                        uid, //该平台用户ID
                        product_id,//兑换Level对应的levelId
                        exchangeCode,//兑换码
                        new IApiCallback() {
                            @Override
                            public void onApiSuccess() {
                                //如果操作成功，需要设置返回成功标志，否则无法刷新书架绘本带锁状态
                                setResult(PaymentJump.RESULT_SUCCESS);

                                Toast.makeText(ExchangeCodeActivity.this, "兑换成功", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onApiFail(String errCode, String errMsg) {
                                Toast.makeText(ExchangeCodeActivity.this, errMsg + " errCode:" + errCode, Toast.LENGTH_SHORT).show();
                            }
                        }
                );
            }
        });
    }

    public void finishView(View view) {
        finish();
    }
}